package com.hckj.jianzhijia;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.avos.avoscloud.UpdatePasswordCallback;
import com.hckj.jianzhijia.controllers.LoginByMesCodeController;
import com.hckj.jianzhijia.controllers.RegisterController;
import com.hckj.jianzhijia.listeners.OnBackClickListener;
import com.hckj.jianzhijia.utils.ExceptionUtils;
import com.hckj.jianzhijia.utils.MyActivityManager;
import com.hckj.jianzhijia.utils.ToastUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 短信登录，这个需要进行手机号码验证过之后才能
 */
public class LoginByMesCodeActivity extends BaseActivity {

    @InjectView(R.id.imgv_go_back)
    ImageView imgvGoBack;
    @InjectView(R.id.edt_phone_num)
    EditText edtPhoneNum;
    @InjectView(R.id.btn_get_code)
    Button btnGetCode;
    @InjectView(R.id.edt_code)
    EditText edtCode;
    @InjectView(R.id.btn_commit)
    Button btnCommit;
    private String phoneNum;
    private String messageCode;
    private LoginByMesCodeController loginByMesCodeController;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_by_mes_code);
        ButterKnife.inject(this);
        loginByMesCodeController = new LoginByMesCodeController(this);
        initEvent();

    }

    private void initEvent() {
        imgvGoBack.setOnClickListener(new OnBackClickListener(this));


        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDataFromEdt();
                if (!loginByMesCodeController.checkAllData(phoneNum, messageCode)) {
                    return;
                }
                AVUser.loginBySMSCodeInBackground(messageCode, messageCode, new LogInCallback<AVUser>() {
                    @Override
                    public void done(AVUser avUser, AVException e) {
                        if (e == null) {
                            if (avUser.getInt("userType") != 1) {
                                ToastUtils.showToast(getApplicationContext(), "该账号为用户版账号，请用企业版账号登录");
                                avUser.logOut();
                            } else {
                                ToastUtils.showToast(getApplicationContext(), "登陆成功");
                                MyActivityManager.getInstance().finishAllActivity();
                                startActivity(new Intent(getApplicationContext(), MainActivity.class));
                            }
                        } else {
                            ToastUtils.showToast(getApplicationContext(), ExceptionUtils.getExceptionInfo(e));
                        }
                    }
                });


            }
        });

        btnGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                getDataFromEdt();
                if (!loginByMesCodeController.checkPhoneNum(phoneNum)) {
                    return;
                } else {
                    CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) {
                        public void onTick(long millisUntilFinished) {
                            btnGetCode.setText("倒计时" + millisUntilFinished / 1000);
                        }

                        public void onFinish() {
                            btnGetCode.setText("重新获取");
                        }
                    };
                    countDownTimer.start();
                    AVUser.requestLoginSmsCodeInBackground(phoneNum, new RequestMobileCodeCallback() {
                        @Override
                        public void done(AVException e) {
                            if (e != null) {
                                ToastUtils.showToast(getApplicationContext(), ExceptionUtils.getExceptionInfo(e));
                            }
                        }
                    });
                }
            }
        });
    }

    public void getDataFromEdt() {
        phoneNum = edtPhoneNum.getText().toString().trim();
        messageCode = edtCode.getText().toString().trim();

    }


}
